<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${pkg}.dao.${upTumTabNam}Dao">
    <resultMap id="RESULT" type="${pkg}.bean.${upTumTabNam}Do">
		<#list columns as col>
			<#if col.columnKey == "PRI">
		<id column="${col.columnName}" property="${col.lowJavaName}"/>
            <#else>
		<result column="${col.columnName}" property="${col.lowJavaName}"/>
            </#if>
        </#list>
    </resultMap>

    <sql id="TABLE_NAME">${tabNam}</sql>

    <sql id="ALL_COLUMNS">
    <#list columns as col>
        ${col.columnName}<#if col_has_next>,</#if>
    </#list>
    </sql>

    <sql id="INSERT_COLUMNS">
    <#list insertColumns as col>
        ${col.columnName}<#if col_has_next>,</#if>
    </#list>
    </sql>

    <!-- 插入（代码生成） -->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="${pkg}.bean.${upTumTabNam}Do">
        insert into
        <include refid="TABLE_NAME"/>
        (
        <include refid="INSERT_COLUMNS"/>
        )
        values
        (
        <#list insertColumns as col>
            #${"\{"}${col.lowJavaName}}<#if col_has_next>, </#if>
        </#list>
        )
    </insert>

    <!-- 根据id查询（代码生成） -->
    <select id="getById" resultMap="RESULT">
        select
        <include refid="ALL_COLUMNS"/>
        from
        <include refid="TABLE_NAME"/>
        where id = #${"\{"}id}
    </select>

    <!-- 批量插入（代码生成） -->
    <insert id="batchInsert">
        insert into
        <include refid="TABLE_NAME"/>
        (
        <include refid="INSERT_COLUMNS"/>
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            <#list insertColumns as col>
                #${"\{"}item.${col.lowJavaName}}<#if col_has_next>, </#if>
            </#list>
            )
        </foreach>
    </insert>

    <!-- 更新（代码生成） -->
    <update id="updateById">
        update
        <include refid="TABLE_NAME"/>
        set
        <#list insertColumns as col>
        <if test="${col.lowJavaName} != null">
            ${col.columnName} = #${"\{"}${col.lowJavaName}},
        </if>
        </#list>
        created_time=created_time
        where id = #${"\{"}id}
    </update>

    <!-- 删除（代码生成） -->
    <delete id="deleteById">
        delete from
        <include refid="TABLE_NAME"/>
        where id = #${"\{"}id}
    </delete>

    <!-- 符合条件总条数（代码生成） -->
    <select id="countTotal" resultType="java.lang.Integer">
        select count(id) cnt from
        <include refid="TABLE_NAME"/>
        where 1=1
        <#list insertColumns as col>
        <#if col.javaType == "String">
        <if test="${col.lowJavaName} != null and ${col.lowJavaName} != ''">
            and ${col.columnName} = #${"\{"}${col.lowJavaName}}
        </if>
        <#elseif col.javaType == "java.util.Date">
		<if test="${col.lowJavaName} != null">
            and ${col.columnName} = #${"\{"}${col.lowJavaName}}
        </if>
        <#else>
		<if test="${col.lowJavaName} != null">
            and ${col.columnName} = #${"\{"}${col.lowJavaName}}
        </if>
        </#if>
        </#list>
    </select>

    <!-- 分页查询（代码生成） -->
    <select id="getPage" resultMap="RESULT">
        select
        <include refid="ALL_COLUMNS"/>
        from
        <include refid="TABLE_NAME"/>
        where 1=1
        <#list insertColumns as col>
        <#if col.javaType == "String">
        <if test="condition != null and condition.${col.lowJavaName} != null and condition.${col.lowJavaName} != ''">
            and ${col.columnName} = #${"\{"}condition.${col.lowJavaName}}
        </if>
        <#elseif col.javaType == "java.util.Date">
		<if test="condition != null and condition.${col.lowJavaName} != null">
            and ${col.columnName} = #${"\{"}condition.${col.lowJavaName}}
        </if>
        <#else>
        <if test="condition != null and condition.${col.lowJavaName} != null">
            and ${col.columnName} = #${"\{"}condition.${col.lowJavaName}}
        </if>
        </#if>
        </#list>
        order by id desc
        limit #${"\{"}offset},#${"\{"}rows}
    </select>
</mapper>  